/**
 * Copyright (c) 2012 Tomi Paananen.
 *
 * Part of Quick Gestures user interface.
 */

import QtQuick 1.1
import com.nokia.symbian 1.1

Page {
    id: fileManagerPage

    property bool saveMode: false
    property string currentFilePath: ""
    property string selectedFilePath: ""

    signal load(variant filePath);
    signal saveTo(variant filePath);

    onCurrentFilePathChanged: {
        var path = helper.removeFileFromPath(currentFilePath);

        if (path.length) {
            console.debug("FileManagerPage.qml: Setting path to", path);
            folderList.folder = path;
        }
    }

    Component {
        id: replaceFileQueryDialog

        QueryDialog {
            height: 250
            titleText: "Replace file"
            message: "Are you sure you want to replace the content of file "
                     + helper.extractFilenameFromPath(selectedFilePath)
                     + " with the current set?";
            acceptButtonText: "Ok"
            rejectButtonText: "Cancel"

            onAccepted: {
                saveTo(selectedFilePath);
                pageStack.pop();
            }
        }
    }

    Loader {
        id: loader
        onLoaded: item.open();
    }

    Rectangle {
        id: saveBar
        height: fileNameField.height + 20

        anchors {
            top: parent.top
            left: parent.left
            right: parent.right
        }

        visible: fileManagerPage.saveMode
        color: "black"
        z: 1

        Row {
            id: saveBarRow

            anchors {
                fill: parent
                margins: 10
            }

            spacing: 10

            TextField {
                id: fileNameField
                width: parent.width - saveButton.width - saveBarRow.spacing
            }
            Button {
                id: saveButton
                text: "Save"
                enabled: fileNameField.text.length

                onClicked: {
                    // Emit signal containing the file path and pop this
                    // page from the stack.
                    var filePath =
                            helper.ensurePathValidity(folderList.folder + "/"
                                                      + fileNameField.text);
                    saveTo(filePath);
                    pageStack.pop();
                }
            }
        }
    }

    FolderList {
        id: folderList

        anchors {
            top: saveMode ? saveBar.bottom : parent.top
            left: parent.left
            right: parent.right
            bottom: parent.bottom
        }

        onFileSelected: {
            console.debug("FileManagerPage.qml: FolderList::onFileSelected:", file);

            if (saveMode) {
                selectedFilePath = file;

                if (loader.item) {
                    loader.item.open();
                }
                else {
                    loader.sourceComponent = replaceFileQueryDialog;
                }
            }
            else {
                load(file);
                pageStack.pop();
            }
        }

        ScrollDecorator {
            flickableItem: folderList.listView
        }
    }

    tools: ToolBarLayout {
        ToolButton {
            iconSource: "toolbar-back"
            onClicked: pageStack.pop();
        }
        ToolButton {
            iconSource: "qrc:/folder_up_icon.png"
            onClicked: folderList.toParentFolder();
            enabled: folderList.hasParentFolder
            opacity: enabled ? 1.0 : 0.3
        }
    }
}
